﻿@model ZDSoft.UPMS.Model.SystemPart.Role
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <title>Update</title>
</head>
<body>
        <script src="../../Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
    <script src="../../Scripts/PageCreater/Scripts/PageCreater.js" type="text/javascript"></script>
    <link href="../../Scripts/PageCreater/Css/Style.css" rel="stylesheet" type="text/css" />


    <script src="../../Scripts/jquery.ztree.core-3.5.js" type="text/javascript"></script>
    <link href="../../Scripts/zTree/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" type="text/css" />
    <script src="../../Scripts/jquery.ztree.excheck-3.5.js" type="text/javascript"></script>
    <script src="../../Scripts/jquery.ztree.exedit-3.5.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            var str = "Name,角色名,false,text,notnull,"
                        + "IsActive,状态,false,select|激活!true|注销!false,notnull,"
                        + "Opreate,操作,false,null,null";
            CreatePageFramWithValue(str, "/Role/GetById");
            $.fn.zTree.init($("#sysTree"), sysSetting, sysNodes);
        })
        var sysSetting = {
            async: {
                enable: true,
                url: "/Role/GetAllSysFunctions",
                type: "Get",
                dataType: "json",
                autoParam: ["id", "pid", "name", "level"],
                otherParam: { "roleID": getUrlPara() }
            },
            check: {
                enable: true
            },
            callback: {
                onAsyncSuccess: function (event, treeId, treeNode, msg) {
                },
                onAsyncError: function (event, treeId, treeNode, XMLHttpRequest, textStatus, errorThrown) {
                    debugger;
                    alert("加载错误，请联系系统管理员！");
                },
                beforeExpand: beforeExpand,
                onExpand: onExpand,
                onClick: onClick,
                onCheck: function (event, treeId, treeNode) {
                    if (treeNode.checked == true) {
                        if (treeNode.level == 0) {
                            if (treeNode.children == null) {
                                treeNode.checked = false;
                                alert("请先展开列表！");
                            }
                        }
                    }
                }
            }
        };
        function createNodes(maxNodesNumInLevel, maxLevel, curLevel, curPId) {
            if (maxNodesNumInLevel < 5) {
                maxNodesNumInLevel = 5;
            }
            var nodes = [], num = 0;
            while (num < 3) {
                num = parseInt(Math.random() * 1024) % maxNodesNumInLevel + 1;
            }
            for (var i = 0; i < num; i++) {
                var id = curPId ? curPId + "-" + i : "" + i, isParent = (parseInt(Math.random() * 9999) % 3 != 0),
				node = { id: id, pId: curPId, name: "N" + id };
                nodes.push(node);
                if (isParent && curLevel < maxLevel) {
                    nodes = nodes.concat(createNodes(maxNodesNumInLevel, maxLevel, curLevel + 1, id));
                }
            }
            return nodes;
        }
        var zNodes = createNodes(5, 5, 0);

        var curExpandNode = null;
        function beforeExpand(treeId, treeNode) {
            var pNode = curExpandNode ? curExpandNode.getParentNode() : null;
            var treeNodeP = treeNode.parentTId ? treeNode.getParentNode() : null;
            var zTree = $.fn.zTree.getZTreeObj("sysTree");
            for (var i = 0, l = !treeNodeP ? 0 : treeNodeP.children.length; i < l; i++) {
                if (treeNode !== treeNodeP.children[i]) {
                    zTree.expandNode(treeNodeP.children[i], false);
                }
            }
            while (pNode) {
                if (pNode === treeNode) {
                    break;
                }
                pNode = pNode.getParentNode();
            }
            if (!pNode) {
                singlePath(treeNode);
            }

        }
        function singlePath(newNode) {
            if (newNode === curExpandNode) return;
            if (curExpandNode && curExpandNode.open == true) {
                var zTree = $.fn.zTree.getZTreeObj("sysTree");
                if (newNode.parentTId === curExpandNode.parentTId) {
                    zTree.expandNode(curExpandNode, false);
                } else {
                    var newParents = [];
                    while (newNode) {
                        newNode = newNode.getParentNode();
                        if (newNode === curExpandNode) {
                            newParents = null;
                            break;
                        } else if (newNode) {
                            newParents.push(newNode);
                        }
                    }
                    if (newParents != null) {
                        var oldNode = curExpandNode;
                        var oldParents = [];
                        while (oldNode) {
                            oldNode = oldNode.getParentNode();
                            if (oldNode) {
                                oldParents.push(oldNode);
                            }
                        }
                        if (newParents.length > 0) {
                            zTree.expandNode(oldParents[Math.abs(oldParents.length - newParents.length) - 1], false);
                        } else {
                            zTree.expandNode(oldParents[oldParents.length - 1], false);
                        }
                    }
                }
            }
            curExpandNode = newNode;
        }

        function onExpand(event, treeId, treeNode) {
            curExpandNode = treeNode;
        }

        function onClick(e, treeId, treeNode) {
            var zTree = $.fn.zTree.getZTreeObj("sysTree");
            zTree.expandNode(treeNode, null, null, null, true);
        }
        var sysNodes = [
			{ id: 1, pid: 0, name: "请选择系统功能", isParent: true, icon: "../../Scripts/zTree/css/zTreeStyle/img/diy/users.png" }
		];

        function SubmitPage() {
            var nodes = $.fn.zTree.getZTreeObj("sysTree").getCheckedNodes(true);
            var selectIDs = "";
            for (var i = 0; i < nodes.length; i++) {
                var xxx = nodes[i];
                selectIDs += nodes[i].tld + ",";
            }
            var roles = { id: getUrlPara(), Name: function () { return $("#Name").val(); }, IsActive: function () { return $("#IsActive").val(); }, systemfunctionId: selectIDs };
            alert(roles);
            $.ajax({
                type: "Post",
                url: "/Role/UpdateS",
                data: roles,
                success: function () {
                    alert("修改成功！");
                    window.location = "/Role/Index";
                }
            });
        }
    </script>


    <div class="Content">
        <fieldset>
            <legend>请输入基本信息</legend>
            <div id="divContent">
            </div>
        </fieldset>
        <fieldset>
            <legend>请配置系统功能信息</legend>
            <ul id="sysTree" class="ztree">
            </ul>
            <p>
                <input id="btnSubmit" type="button" value="保存" class="btn3" onclick="SubmitPage()" />
            </p>
        </fieldset> 
        <div>
            @Html.ActionLink("返回主页面", "Index")
        </div>
    </div>
</body>
</html>
